package br.com.passeionaweb.appengine.tabelafipe.data;

import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;


@PersistenceCapable
public class YearVersion {
	@PrimaryKey
	@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
	private Long id;

	@Persistent
	private Long yearid;

	@Persistent
	private Long versionid;

	public YearVersion(Long yearid, Long versionid) {
		super();
		this.yearid = yearid;
		this.versionid = versionid;
	}

	public Long getYearid() {
		return yearid;
	}

	public void setYearid(Long yearid) {
		this.yearid = yearid;
	}

	public Long getVersionid() {
		return versionid;
	}

	public void setVersionid(Long versionid) {
		this.versionid = versionid;
	}

	public Long getId() {
		return id;
	}
	
	public static boolean exists(Year year, Version version){
		return getYearVersion(year,version) != null;
	}
	
	public static YearVersion getYearVersion(Year year, Version version){
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Query q = pm.newQuery(YearVersion.class);
		q.setFilter("versionid == param_versionid && yearid == param_yearid");
		q.declareParameters("Long param_versionid, Long param_yearid");
		List<YearVersion> yearversions = (List<YearVersion>) q.execute(version.getId(),year.getId());
		if(yearversions.isEmpty()){
			return null;
		}else{
			return yearversions.get(0);
		}
	}
	
}
